<?php
/**
 * @author Nguyen Thanh Tung
 * @copyright 2014
 * @description: save action sample
 */
// not login
// not perm add
if( !isset($AppUI) ||
	!isset($_SESSION['uniform']) ||
	(isset($_SESSION['uniform']) && ($_SESSION['uniform']&1<<2)==0)	
){	
/*|| 
	(isset($AppUI->id) && !in_array($AppUI->type, array(ROLE_ROOT, ROLE_ADMINISTRATOR)))*/
	redirect(HOST_BACKEND);
}
// data input
$intClubId        = $this->input->post('clubsid',0);
$intUniformId	  = $this->input->post('uniformid',0);	
$intFrom          = $this->input->post('pfrom',0);//test($intFrom);
$intTo            = $this->input->post('pto',0);			
$intHomeShirt     = $this->input->post('phomeshirt',0);
$strShort         = $this->input->post('pshort', null);
$strDescription   = $this->input->post('pdescription', null);
$arrErr           = array();

// strip tag data input
$strShort        = CI_Function::stripTag($strShort);
$lenShort        = strlen($strShort);

$strDescription   = CI_Function::stripTag($strDescription);
$lenDescription   = strlen($strDescription);

// validate manual	
//+ short	
if(!isset($strShort) || empty($strShort))
    array_push($arrErr, $arrLocale['short_required']);
elseif(isset($strShort) && ($lenShort < VL_NAME_MINLENGTH || $lenShort > VL_NAME_STADIUM_MAXLENGTH) ) 
    array_push($arrErr, $arrLocale['short_range']);
//+ description	
if(!isset($strDescription) || empty($strDescription))
    array_push($arrErr, $arrLocale['description_required']);
elseif(isset($strDescription) && ($lenDescription < VL_CONTENT_MINLENGTH) ) 
    array_push($arrErr, $arrLocale['description_minlength']);

// arrErr ==0 allow save
if(isset($arrErr) && count($arrErr)==0) {
    // unset session if exists
    if(isset($_SESSION['UNIFORM_ERROR'])) unset($_SESSION['UNIFORM_ERROR']);
    if(isset($_SESSION['UNIFORM_POST'])) unset($_SESSION['UNIFORM_POST']);
    $this->load->model('uniform_model', 'uniform');	
    $intError = $this->uniform->update($AppUI->id, $intUniformId, $intClubId, $intFrom, $intTo, $intHomeShirt, $strShort, $strDescription);
    if($intError==0) CI_Function::setMsg($arrMessage['save_update']);
    elseif($intError==1) array_push($arrErr, $arrLocale['noperm_save']);
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
    $_SESSION['UNIFORM_ERROR']    = $arrErr;    
    $_SESSION['UNIFORM_POST']     = $this->input->post();
    
    redirect(HOST_BACKEND . 'uniform/edit?uniformid='.$intUniformId);
}
redirect(HOST_BACKEND.'uniform/index?src=0&id='.$intClubId);
